import { Component, OnInit, Output, EventEmitter, Inject, forwardRef, ChangeDetectorRef, ChangeDetectionStrategy } from '@angular/core';
import { path } from '../../../../path.config';
import { masterService } from '../../../../../lib/master.service';
import { GridComponent } from '../grid-grade/grid.component';
import { AppComponent } from '../../../../app.component';

import * as $ from 'jquery';

@Component({
    selector: 'prepaid-voucher-form',
    styleUrls: ['./prepaid-voucher.component.css'],
    templateUrl: './prepaid-voucher.component.html',
    providers: [GridComponent, forwardRef(() => AppComponent)],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class PrepaidVoucherComponent implements OnInit {
    @Output() closeModal = new EventEmitter<any>();

    public getList: any = path.accountRecharge.prepaidVoucher.getGradeList;
    // 获取优惠券名称
    private getVoucherName = path.accountRecharge.prepaidVoucher.getVoucherName;
    // 创建充值档
    private createGrade = path.accountRecharge.prepaidVoucher.createGrade;
    // 检查商品编号
    public checkSu = path.accountRecharge.prepaidVoucher.checkSu;
    // 添加新优惠券对象模型
    public rows = [ ];

    public condition = {
        suGuid: '',
        amount: '',
        displayAmount: '',
        items: [],
        displayIndex: ''
    };

    private reChargeable = '';
    private reChargeableInfo = '';

    constructor(private _masterService: masterService,
        public cdRef: ChangeDetectorRef,
        @Inject(GridComponent) public grid: GridComponent,
        @Inject(forwardRef(() => AppComponent)) private app: AppComponent) { }

    ngOnInit() { }

    // 添加新建行
    addNewPrePaid() {
        this.rows.push({
            couponId: '',
            name: '',
            count: '',
            displayName: '',
            goodsNum: '',
            goodsName: ''
        })
    }

    // 删除新建行
    delPrePaid(rowId) {
        let delIndex: number;
        this.rows.forEach((val, index) => {
            if (rowId == val.couponId) {
                delIndex = index;
            }
        })
        this.rows.splice(delIndex, 1);
    }

    // id联动名称
    showName(id, index) {
        this._masterService
            .getData(this.getVoucherName, { id: id })
            .subscribe((res) => {
                if (res.data) {
                    $('#name' + index + index).val(res.data.name);
                    this.rows[index].name = $('#name' + index + index).val();
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }

    saveAdd() {
        // 前端验证
        if (this.condition.suGuid == '') {
            this.app.toastWarning('请填写商品编号！');
            return;
        } else if (this.condition.amount == '') {
            this.app.toastWarning('请填写充值档金额！');
            return;
        } else if (this.condition.displayIndex == '') {
            this.app.toastWarning('请输入排序大小！');
            return;
        }
        for (let i = 0; i < this.rows.length; i++) {
            if (this.rows[i].count == '' && this.rows[i].couponId != '') {
                this.app.toastWarning('请填写数量！');
                return;
            } else if (this.rows[i].displayName == '' && this.rows[i].couponId != '') {
                this.app.toastWarning('请填写前台显示名称！');
                return;
            } else if (this.rows[i].couponId == '' && this.rows[i].name == '' && this.rows[i].count == '' && this.rows[i].displayName == '') {
                this.app.toastWarning('添加的券信息不能为空！');
                return;
            }
        }
        if (!(/(^[1-9]\d*$)/.test(this.condition.displayIndex))) {
            this.app.toastWarning('排序请输入大于零的整数！');
            return;
        }
        if (this.condition.displayAmount && this.condition.displayAmount.length > 15) {
            this.app.toastWarning('前台显示礼包总额不能超过15个字！');
            return;
        }
        let urlParams = {
            suGuid: this.condition.suGuid
        }
        this._masterService
            .getData(this.checkSu, urlParams)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.condition.items = this.rows;
                    this._masterService
                        .postData(this.createGrade, this.condition)
                        .subscribe((res) => {
                            if (res.statusCode == 200) {
                                this.app.toastSuccess('添加成功！');
                                this.condition = {
                                    suGuid: '',
                                    amount: '',
                                    displayAmount: '',
                                    items: [],
                                    displayIndex: ''
                                }
                                this.closeModal.emit();
                            } else {
                                this.app.toastError('错误信息： ' + res.errorMessage);
                                return;
                            }
                        });
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }
}
